package com.echo.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import java.io.InputStream;

/**
 * Created by EchoGe
 *图片操作工具类
 */
public class BitmapUtils {

	public static Bitmap readBitMap(Context context, int resId){
		     BitmapFactory.Options opt = new BitmapFactory.Options();
		     opt.inPreferredConfig = Config.RGB_565;
		     opt.inPurgeable = true;
		     opt.inInputShareable = true;
		        //获取资源图片
		     InputStream is = context.getResources().openRawResource(resId);
		         return BitmapFactory.decodeStream(is,null,opt);
	}



	/**
	 *
	 * 等比例根据x坐标压缩
	 * */
	public static Bitmap getSmallBitmapByX(Bitmap bitmap, float f) {
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scale2 = (float) f / w;
		matrix.postScale(scale2, scale2);
		return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
	}

	/**
	 *
	 * 等比例根据y坐标压缩
	 * */
	public static Bitmap getSmallBitmapByY(Bitmap bitmap, float y) {
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scale2 = (float) y / h;
		matrix.postScale(scale2, scale2);
		return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
	}
	/**
	 * 旋转Bitmap
	 * @param b
	 * @param rotateDegree
	 * @return
	 */
	public static Bitmap getRotateBitmap(Bitmap b, float rotateDegree){
		Matrix matrix = new Matrix();
		matrix.postRotate((float)rotateDegree);
		Bitmap rotaBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, false);
		return rotaBitmap;
	}
	
/**
 * 变形压缩图片
 * */
	public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
			double newHeight) {
		float width = bgimage.getWidth();
		float height = bgimage.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
				(int) height, matrix, true);
		return bitmap;
	}
	
}
